var packageName = 'com.ss.android.ugc.aweme'

// 打开视频App
launch(packageName);
toast('打开抖音')

// App加载完成
waitForPackage(packageName);
toast('加载完成')
sleep(6000)

// 点击消息
if(text("以后再说").className("android.widget.TextView").depth("7").drawingOrder("1").findOne(1000)){
    text("以后再说").className("android.widget.TextView").depth("7").drawingOrder("1").click() 
}
const xx = className("android.widget.TextView").depth("19").drawingOrder("1").text("消息").findOne(1000)
if(xx){
    log('找到消息')
    click(xx.bounds().centerX(),xx.bounds().centerY())
}else{
    toast('没找到消息');
    console.log('没找到消息');
    back()
}
sleep(random(1000,3000))
// 自动回复评论
// findMsg()
// river()
keywordSearch()

// 生成随机数函数
function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function findMsg (){
    const msg = id("su7").className("android.widget.TextView").depth("22").drawingOrder("2").findOne(2000)
    if(msg){
        // 有新消息
        toast('有新消息');
        click(msg.bounds().centerX(),msg.bounds().centerY())
        const text = className("android.widget.TextView").depth("17").drawingOrder("2").findOne(2000).text()
        console.log('新消息'+ text);
        if(text){
            const input = className("android.widget.EditText").depth("13").drawingOrder("6").text("发送消息").findOne(2000)
            input.setText(1)
            className("android.widget.ImageView").desc("发送").depth("13").click()
            back()
            findMsg()
        }else{
            toast('没有text');
            back()
        }

    }else{
        const not = className("android.widget.TextView").depth("20").drawingOrder("2").text("暂时没有更多了").findOne(1000)
        if(not){
            // 没有新消息
            toast('没有新消息');
            river()
        }else{
            const moShengRen2 = className("android.widget.ImageView").depth("22").drawingOrder("1").findOne(1000)
            if(moShengRen2&&moShengRen2.parent().parent().parent().child(1).child(0).text()=="陌生人消息"){
                toast('有陌生人消息')
                click(moShengRen.bounds().centerX(), moShengRen.bounds().centerY())
                sleep(random(1000,3000))
                moShengRen()
            }
            const ping = className("android.widget.LinearLayout").drawingOrder("1").depth("1").findOne()
            swipe(ping.bounds().centerX(), ping.bounds().centerY() + 250, ping.bounds().centerX(), ping.bounds().centerY() - 250, random(500,2000))
            findMsg()
        }
        
    }
}



// 陌生人回复
function moShengRen() {
    const moSRxx = className("android.widget.TextView").depth("12").drawingOrder("2").findOne(2000)
        if(moSRxx){
            click(moSRxx.bounds().centerX(), moSRxx.bounds().centerY())
            const jixu = className("android.widget.TextView").depth("12").drawingOrder("1").text("要继续与").findOne(2000)
            if(jixu){
                className("android.widget.Button").depth("12").drawingOrder("3").text("确认").findOne(2000).click()
                const jixuInput = className("android.widget.EditText").depth("14").text("发送消息").findOne(2000)
                jixuInput.setText(1)
                sleep(random(500,2000))
                className("android.widget.ImageView").desc("发送").depth("14").findOne(2000).click()
                back()
                moShengRen()
            }else{
                const jixuInput1 = className("android.widget.EditText").depth("14").text("发送消息").findOne(2000)
                if(jixuInput1){
                    jixuInput1.setText(1)
                    sleep(random(500,2000))
                    className("android.widget.ImageView").desc("发送").depth("14").findOne(2000).click()
                    back()
                    moShengRen()
                }else{
                    toast('没有输入框')
                    back()
                }
            }
        }else{
            console.log('没找到陌生人消息');
            swipe(device.width/2, device.height/2 + 250, device.width/2, device.height/2 - 250, random(500,2000))
            const wu = className("android.widget.TextView").text("暂时没有更多了").depth("8").drawingOrder("2").findOne(2000)
            if(wu){
                toast('没有陌生人消息')
                back()
            }else{
                console.log('陌生人消息');
                moShengRen()
            }
        }
}


let FaBuTime = 0
/**
 * 截流函数
 *
 * @returns 无返回值
 */
function river () {
    const keywordID = 1
    className("android.widget.Button").desc("搜索").depth("18").drawingOrder("1").findOne(1000).click()
    if(keywordID){
        className("android.widget.EditText").depth("6").drawingOrder("3").text("搜索").findOne().setText('餐饮')
        sleep(random(1000,3000))
        const serch = text("视频、用户、音乐、话题、地点等").drawingOrder("5").findOne(3000)
        if(serch){
            toast('找到搜索')
        }
        click(serch.bounds().centerX(), serch.bounds().centerY())
        let itemDouYin = className("android.widget.LinearLayout").depth("23").drawingOrder("1").find()
        itemDouYin = itemDouYin.filter(e=>e.child(0).child(1).childCount()>=2)
        click(itemDouYin[0].child(1).child(0).child(1).bounds().centerX(),itemDouYin[0].child(1).child(0).child(1).bounds().centerY())
        sleep(random(2000,3000))
        findKeyword()
    }else{
        const arr = ['52479686536']
        arr.forEach(e=>{
            className("android.widget.EditText").depth("6").drawingOrder("3").text("搜索").findOne().setText(e)
            sleep(random(1000,3000))
            const serch = text("视频、用户、音乐、话题、地点等").drawingOrder("5").findOne(3000)
            if(serch){
                toast('找到搜索')
            }
            click(serch.bounds().centerX(), serch.bounds().centerY())
            sleep(random(2000,3000))
            const user = className("android.widget.Button").text("用户").depth("16").findOne(1000)
            click(user.bounds().centerX(), user.bounds().centerY())
            sleep(random(500,1000))
            const userHao = className("com.lynx.tasm.behavior.ui.LynxFlattenUI").depth("19").drawingOrder("1").findOne(2000)
            if(userHao){
                toastLog('找到用户')
                // console.log(userHao.child(0).text());
                click(userHao.bounds().centerX(), userHao.bounds().centerY())
                sleep(random(2000,3000))
                className("android.view.View").depth("12").drawingOrder("1").findOne(2000).click()
                sleep(random(1000,3000))
                three()
            }else{
                toastLog('没有找到用户')
            }
        })
    }
    
}

function three() {
    sleep(random(1000,3000))
    let videoTimeList = className("android.widget.TextView").depth("23").drawingOrder("1").find()
    console.log(videoTimeList.length);
    videoTimeList.forEach(e=>{
        console.log(e.text());
    })
    videoTimeList = videoTimeList.filter(e=>{
        return e.text().includes('IP属地')
    })
    let videoTime = videoTimeList[0].text()
    videoTime = videoTime.split('I')
    console.log(videoTime[0].trim());
    // 全屏
    const x = className("android.widget.FrameLayout").depth("0").drawingOrder("0").findOne(1000)
        if(videoTime[0].trim() == FaBuTime){
            toastLog('没有视频了')
            console.log('时间');
            console.log(FaBuTime);
            back()
            sleep(random(1000,3000))
            back()
            sleep(random(1000,3000))
            back()
            return
        }else{
            console.log('不是最后一个视频');
            FaBuTime = videoTime[0].trim()
        }
        toast('视频时间在三天内')
        const pingLun = className("android.widget.LinearLayout").depth('21').drawingOrder('1').find()
        if(pingLun[1]){
            if(pingLun[1].child(1).child(0).text()=="评论"){
                toast('没有评论')
                swipe(
                    device.width / 2,
                    device.height * 0.8,
                    device.width / 2,
                    device.height * 0.2,
                    random(500,1000)
                )
                three()
            }else{
                pingLun[1].click()
                jieLiu()
                three()
            }
        }
}


function findKeyword () {
    const guangGaoDY = className("android.widget.TextView").depth("24").drawingOrder("1").text('广告').findOne(2000)
    if(!guangGaoDY){
      const keywordNameDY = className("android.widget.TextView").depth('22').drawingOrder('3').desc('按钮').findOne(2000) 
      const keywordDayDY = className("android.widget.TextView").depth('26').drawingOrder('1').findOne(2000) 
      const keywordTitle = className("android.widget.TextView").depth("24").desc('按钮').drawingOrder("1").findOne(2000)
      console.log(keywordNameDY.text());
      console.log(keywordDayDY.text());
      console.log(keywordTitle.text());
      const pingLun = className("android.widget.LinearLayout").depth('21').drawingOrder('1').find()
      if(pingLun[1]){
          if(pingLun[1].child(1).child(0).text()=="评论"){
              toast('没有评论')
              swipe(
                  device.width / 2,
                  device.height * 0.8,
                  device.width / 2,
                  device.height * 0.2,
                  random(500,1000)
              )
              findKeyword()
          }else{
              pingLun[1].click()
              jieLiuKeyword()
              sleep(random(1000,2000))
              swipe(
                device.width / 2,
                device.height * 0.8,
                device.width / 2,
                device.height * 0.2,
                random(500,1000)
              )
              findKeyword()
          }
      }
    }
    swipe(
      device.width / 2,
      device.height * 0.8,
      device.width / 2,
      0,
      random(500,1000)
    )
    sleep(random(1000,2000))
    findKeyword()
}
function jieLiuKeyword(){
  sleep(random(2000,3000))
  const list = className("android.widget.TextView").depth("15").drawingOrder("27").find()
  const arr = []
  list.forEach(e=>{
      arr.push(e.parent().child(1).bounds().height())
  })
  list.forEach(e=>{
      // 筛选掉最后一个
      sleep(random(2000,3000))
      if(e.parent().child(1).bounds().height() < findMax(arr)) return
      // 评论
      console.log(e.parent().child(2).text());
      console.log(e.parent().child(4).child(1).text());
      console.log(e.text());
      click(e.parent().child(1).bounds().centerX(), e.parent().child(1).bounds().centerY())
      sleep(random(2000,3000))
      const DouYinHao = className("android.widget.TextView").depth("12").drawingOrder("2").findOne(2000)
      let IPDouYin = className("android.widget.TextView").depth("12").find()
      IPDouYin = IPDouYin.filter(e=>e.text().includes('IP'))
      if(DouYinHao&&IPDouYin.length){
        console.log(DouYinHao.text());
        console.log(IPDouYin[0].text()); 
      }
      back()
      sleep(random(1000,3000))
  })
  if(className("android.widget.TextView").depth("14").drawingOrder("2").text("暂时没有更多了").findOne(2000)){
      toast('没有评论了')
      back()
  }else{
      const PingLunBox = className("android.widget.FrameLayout").depth("6").drawingOrder("2").findOne(2000)
      swipe(
        device.width / 2,
        device.height * 0.8,
        device.width / 2,
        device.height * 0.2,
        random(1000,2000)
      )
      jieLiuKeyword()
  }
}

function findMax(array) {
  let max = array[0];
  for (let i = 1; i < array.length; i++) {
      if (array[i] > max) {
          max = array[i];
      }
  }
  return max;
}

function jieLiu(){
    const list = className("android.widget.TextView").depth("15").drawingOrder("27").find()
    list.forEach(e=>{
        // 筛选掉最后一个
        if(e.parent().child(1).bounds().height() < list[3].parent().child(1).bounds().height()) return
        // 评论
        console.log(e.text());
        click(e.parent().child(1).bounds().centerX(), e.parent().child(1).bounds().centerY())
        sleep(random(2000,3000))
        const DouYinHao = className("android.widget.TextView").depth("12").drawingOrder("2").findOne(2000)
        const IPDouYin = className("android.widget.TextView").depth("12").drawingOrder("1").findOne(2000)
        console.log(DouYinHao.text());
        console.log(IPDouYin.text());
        back()
        sleep(random(1000,3000))
        // if(e.text() == '10'){
        //     click(e.parent().child(1).bounds().centerX(), e.parent().child(1).bounds().centerY())
        //     sleep(random(1000,2000))
        //     className("android.widget.ImageView").depth("8").desc("更多").findOne(1000).click()
        //     sleep(random(500,1000))
        //     const FaSiXin = className("android.widget.TextView").depth("14").drawingOrder("2").text("发私信").findOne(1000)
        //     click(FaSiXin.bounds().centerX(), FaSiXin.bounds().centerY())
        //     sleep(random(1000,2000))
        //     const FaSonginput = className("android.widget.EditText").depth("14").drawingOrder("6").text("发送消息").findOne(1000)
        //     FaSonginput.setText(1)
        //     className("android.widget.ImageView").depth("14").desc("发送").findOne(1000).click()
        //     back()
        //     sleep(random(1000,2000))
        //     back()
        // } 
    })
    if(className("android.widget.TextView").depth("14").drawingOrder("2").text("暂时没有更多了").findOne(2000)){
        toast('没有评论了')
        back()
    }else{
        const PingLunBox = className("android.widget.FrameLayout").depth("6").drawingOrder("2").findOne(2000)
        swipe(PingLunBox.bounds().centerX(), PingLunBox.bounds().centerY(),device.width/2,device.height*1/3,1000 )
        jieLiu()
    }
}


/**
 * 判断给定时间字符串是否在三天内
 *
 * @param timeStr 时间字符串，格式为"YYYY-MM-DD HH:MM"
 * @returns 返回布尔值，若在三天内返回true，否则返回false
 */
function isTimeWithinThreeDays(timeStr) {
    console.log(timeStr);
    // 当前时间
    const currentTime = new Date();
    const inputTime = new Date(timeStr.replace(' ', 'T') + ':00')
  
    // 如果解析失败，则返回false
    if (isNaN(inputTime.getTime())) {
      console.error("无效的时间格式");
      return false;
    }
  
    // 计算当前时间与目标时间之间的时间差（毫秒）
    const timeDifference = currentTime - inputTime;
  
    // 三天的时间量（毫秒）
    const threeDays = 3 * 24 * 60 * 60 * 1000;
  
    // 检查时间差是否在三天内
    if (timeDifference < threeDays) {
      return true; // 在三天内
    } else {
      return false; // 不在三天内
    }
  }


function huiFuPingLun (){
    const redText = className("android.widget.TextView").depth("22").drawingOrder("1")
    if(redText.parent().parent().child(1).child(0).text() == '互动消息'){
        click(redText.parent().bounds().centerX(), redText.parent().bounds().centerY())
        sleep(random(1000,2000))
        const FenLei = className("android.widget.TextView").depth("9").drawingOrder("2").text("全部消息").findOne(1500)
        click(FenLei.bounds().centerX(), FenLei.bounds().centerY())
        sleep(random(500,1000))
        className("android.view.ViewGroup").depth("4").desc("收到的评论").drawingOrder("4").findOne(1500).click()
    }
}

function zhaoHongDian (){
    while(true){
        const redPoint = className("android.view.View").depth("13").drawingOrder("2")
    }
}

function keywordSearch(){
    className("android.widget.Button").desc("搜索").depth("18").drawingOrder("1").findOne(1000).click()
    sleep(random(1000,3000))
    className("android.widget.EditText").depth("6").drawingOrder("3").text("搜索").findOne().setText('餐饮')
    sleep(random(1000,3000))
    const serch = text("视频、用户、音乐、话题、地点等").drawingOrder("5").findOne(3000)
    if(serch){
        toast('找到搜索')
    }
    click(serch.bounds().centerX(), serch.bounds().centerY())
    sleep(random(2000,3000))
    const itemDouYin = className("android.widget.LinearLayout").depth("23").drawingOrder("1").find()
    
}